package com.xfly.app.wxapi;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.ptmall.app.R;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "WXEntryActivity";

    private IWXAPI mApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApi = WXAPIFactory.createWXAPI(this, "wx971b735e6872c3b7", false);
        mApi.registerApp("wx971b735e6872c3b7");
        try {
            mApi.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onReq(BaseReq baseReq) {

    }

    private String mCode = null;                                           // appid   secret    code  grant_type
    private String mUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%S&secret=%s&code=%s&grant_type=%s";

    private static final String mState = WxLogin.WX_LOGIN_STATE;

    private static WxLoginResultListener mWxLoginResultListener = null;

    public static void setWxLoginListener(WxLoginResultListener listener) {
        mWxLoginResultListener = listener;
    }

    @Override
    public void onResp(BaseResp resp) {
        int result = 0;
        switch (resp.errCode) {
            case BaseResp.ErrCode.ERR_OK: {
                result = R.string.errcode_success;
                switch (resp.getType()) {
                    case ConstantsAPI.COMMAND_SENDAUTH: {
                        //微信登录
                        SendAuth.Resp sendResp = (SendAuth.Resp) resp;
                        if (mState != null && mState.equals(sendResp.state)) {
                            Log.i(TAG, sendResp.code);
                            if (null != mWxLoginResultListener) {
                                mWxLoginResultListener.onSuccess(sendResp.code);
                                mWxLoginResultListener = null;
                                WXEntryActivity.this.finish();
                            }
                        }
                        break;
                    }
                    case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX: {
                        //微信分享
                        WxShareInstance.notifyShareResult(WxShareInstance.SHARE_SUCCESS);
                        WXEntryActivity.this.finish();
                        break;
                    }
                }
            }
            break;
            case BaseResp.ErrCode.ERR_USER_CANCEL: {
                result = R.string.errcode_cancel;
                switch (resp.getType()) {
                    case ConstantsAPI.COMMAND_SENDAUTH: {
                        //微信登录取消
                        if (null != mWxLoginResultListener) {
                            mWxLoginResultListener.onCancel();
                            mWxLoginResultListener = null;
                            WXEntryActivity.this.finish();
                        }
                        break;
                    }
                    case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX: {
                        //微信分享取消
                        WxShareInstance.notifyShareResult(WxShareInstance.SHARE_CANCEL);
                        WXEntryActivity.this.finish();
                        break;
                    }
                }
                break;
            }

            case BaseResp.ErrCode.ERR_AUTH_DENIED: {
                result = R.string.errcode_deny;
                switch (resp.getType()) {
                    case ConstantsAPI.COMMAND_SENDAUTH: {
                        //微信登录被拒绝
                        if (null != mWxLoginResultListener) {
                            mWxLoginResultListener.onCancel();
                            mWxLoginResultListener = null;
                            WXEntryActivity.this.finish();
                        }
                        break;
                    }
                    case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX: {
                        //微信分享失败
                        WxShareInstance.notifyShareResult(WxShareInstance.SHARE_FAIL);
                        WXEntryActivity.this.finish();
                        break;
                    }
                }
                break;
            }
            case BaseResp.ErrCode.ERR_UNSUPPORT: {
                result = R.string.errcode_unsupported;
                if (null != mWxLoginResultListener) {
                    mWxLoginResultListener.onError(getString(result));
                    mWxLoginResultListener = null;
                    WXEntryActivity.this.finish();
                }
                break;
            }
        }
    }
}
